package it.univaq.mancoosi.injectors.systemconfiguration.managers;

import it.univaq.mancoosi.injectors.systemconfiguration.managers.ubuntu.UbuntuEnvironmentManager;
import it.univaq.mancoosi.mancoosimm.MancoosiFactory;
import it.univaq.mancoosi.mancoosimm.MenuEntry;
import it.univaq.mancoosi.mancoosimm.MenuSection;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;

public abstract class MenuManager {
	protected static  MenuManager INSTANCE ;	 
	protected MancoosiFactory factory; 
//	private Menu menu;
	protected HashMap<String, MenuEntry> entryMap; 
	protected HashMap<String, MenuSection> sectionMap; 

	// Private constructor prevents instantiation from other classes
	protected MenuManager() {
		factory = MancoosiFactory.eINSTANCE; 
//		menu = factory.createMenu();
		this.entryMap = new HashMap<String, MenuEntry>();
		this.sectionMap = new HashMap<String, MenuSection>();
	}

	public static MenuManager getInstance() {
		return INSTANCE;
	}

	public abstract void createMenuFromSystem();
	
	public abstract Boolean existsEntry(String entryName);
	
	public abstract Boolean existsSection(String sectionName);

	public abstract void synchronize() throws IOException;
	
	public void syncEntryMap () {
		Iterator<MenuEntry> iterator  = null;
		if(UbuntuEnvironmentManager.getInstance().getEnvironment().getMenu() != null){
			iterator = UbuntuEnvironmentManager.getInstance().getEnvironment().getMenu().getEntries().iterator();
		}
		if(iterator != null){
			while (iterator.hasNext()) {
				MenuEntry curr = iterator.next();
				this.entryMap.put(curr.getName(), curr);
			}
		}
	}
	
	public void syncSectionMap () {
		Iterator<MenuSection> iterator  = null;
		if(UbuntuEnvironmentManager.getInstance().getEnvironment().getMenu() != null){
			iterator = UbuntuEnvironmentManager.getInstance().getEnvironment().getMenu().getSections().iterator();
		}
		if(iterator != null){
			while (iterator.hasNext()) {
				MenuSection curr = iterator.next();
				this.sectionMap.put(curr.getEntryName(), curr);
			}
		}
	}	
}
